package com.bawei.androidh5;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {



    Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 1:
                    web_main.loadUrl("javascript:baoming3()"); //aa是js的函数test()的参数
                    break;
            }
        }
    };


    WebView web_main;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        web_main = (WebView) findViewById(R.id.web_main);

        String path = "file:///android_asset/android.html";
        web_main.loadUrl(path);




        //安卓调用H5
        web_main.getSettings().setJavaScriptEnabled(true); //加上这句话才能使用javascript方法
        //H5调用安卓
        web_main.addJavascriptInterface(new PayJavaScriptInterface(), "demo");


        //面朝大海春暖花开

        //你好中国 我要中华




    }





    final class PayJavaScriptInterface {


        PayJavaScriptInterface() {

        }

        @JavascriptInterface
        public String getUserinfo() {


            Toast.makeText(getApplicationContext(),"报名",Toast.LENGTH_LONG).show();

            return "dd";
        }

        @JavascriptInterface
        public boolean needLogin() {

            Toast.makeText(getApplicationContext(),"登陆",Toast.LENGTH_LONG).show();

            return false;
        }

        @JavascriptInterface
        public void getData(String name){

            Toast.makeText(getApplicationContext(),name,Toast.LENGTH_LONG).show();
            //android 调用H5的方法
//            web_main.loadUrl("javascript:baoming3()"); //aa是js的函数test()的参数
//            Message msg = handler.obtainMessage();
//            msg.what = 1;
//            handler.sendMessage(msg);

            web_main.post(new Runnable() {
                @Override
                public void run() {
                    web_main.loadUrl("javascript:baoming3()"); //aa是js的函数test()的参数
                }
            });

        }

    }

}
